package com.ruoyi.mp.controller;

import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.mp.service.IMpAccountService;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.card.CardUpdateResult;
import me.chanjar.weixin.mp.bean.card.WxMpCardCreateResult;
import me.chanjar.weixin.mp.bean.card.membercard.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <h3>springcloudSaas</h3>
 * <p>会员卡</p>
 *
 * @author : 郎兴宇
 * @date : 2024-09-29 14:17
 **/
@RestController
@RequestMapping("/memberCard")
public class MpMemberCardController extends BaseController {

    @Autowired
    private IMpAccountService iMpAccountService;
    @Autowired
    private WxMpService wxService;


    /**
     * 会员卡创建接口
     */
    @RequiresPermissions("mp:memberCard:createMemberCard")
    @Log(title = "会员卡创建接口.", businessType = BusinessType.INSERT)
    @PostMapping("/createMemberCard/{id}")
    public AjaxResult createMemberCard(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        WxMpMemberCardCreateMessage createMessageMessage = new WxMpMemberCardCreateMessage();
        WxMpCardCreateResult wxMpCardCreateResult =  wxService.switchoverTo(appId).getMemberCardService().createMemberCard(createMessageMessage);
        return AjaxResult.success(wxMpCardCreateResult);
    }

    /**
     * 会员卡激活接口.
     */
    @RequiresPermissions("mp:memberCard:activateMemberCard")
    @Log(title = "会员卡激活接口..", businessType = BusinessType.INSERT)
    @PostMapping("/activateMemberCard/{id}")
    public AjaxResult activateMemberCard(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        WxMpMemberCardActivatedMessage activatedMessage = new WxMpMemberCardActivatedMessage();
        String wxMpCardCreateResult  =  wxService.switchoverTo(appId).getMemberCardService().activateMemberCard(activatedMessage);
        return AjaxResult.success(wxMpCardCreateResult);
    }

    /**
     * 获取会员卡开卡插件参数(跳转型开卡组件需要参数)..
     */
    @RequiresPermissions("mp:memberCard:getActivatePluginParam")
    @Log(title = "获取会员卡开卡插件参数(跳转型开卡组件需要参数)", businessType = BusinessType.INSERT)
    @GetMapping("/getActivatePluginParam/{id}")
    public AjaxResult getActivatePluginParam(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId = "123";
        String outStr = "{\"card_id\":\""+cardId+"\"}";
        ActivatePluginParam activatePluginParam  =  wxService.switchoverTo(appId).getMemberCardService().getActivatePluginParam(cardId,outStr);
        return AjaxResult.success(activatePluginParam);
    }


    /**
     * 获取开卡组件链接接口
     */
    @RequiresPermissions("mp:memberCard:getActivatePluginUrl")
    @Log(title = "获取开卡组件链接接口", businessType = BusinessType.INSERT)
    @GetMapping("/getActivatePluginUrl/{id}")
    public AjaxResult 	getActivatePluginUrl(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId = "123";
        String outStr = "{\"card_id\":\""+cardId+"\"}";
        String activatePluginUrl  =  wxService.switchoverTo(appId).getMemberCardService().getActivatePluginUrl(cardId,outStr);
        return AjaxResult.success(activatePluginUrl);
    }

    /** v
     * 解析跳转型开卡字段用户提交的资料
     */
    @RequiresPermissions("mp:memberCard:getActivateTempInfo")
    @Log(title = "解析跳转型开卡字段用户提交的资料", businessType = BusinessType.INSERT)
    @PostMapping("/getActivateTempInfo/{id}")
    public AjaxResult getActivateTempInfo(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String activateTicket = "123";
        WxMpMemberCardActivateTempInfoResult wxMpMemberCardActivateTempInfoResult  =  wxService.switchoverTo(appId).getMemberCardService().getActivateTempInfo(activateTicket);
        return AjaxResult.success(wxMpMemberCardActivateTempInfoResult);
    }


    /**
     * 拉取会员信息接口.
     */
    @RequiresPermissions("mp:memberCard:getUserInfo")
    @Log(title = "拉取会员信息接口.", businessType = BusinessType.INSERT)
    @GetMapping("/getUserInfo/{id}")
    public AjaxResult getUserInfo(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        String cardId = "123";
        String code = "123";
        WxMpMemberCardUserInfoResult wxMpMemberCardUserInfoResult  =  wxService.switchoverTo(appId).getMemberCardService().getUserInfo(cardId,code);
        return AjaxResult.success(wxMpMemberCardUserInfoResult);
    }

    /**
     * 设置会员卡激活的字段（会员卡设置：wx_activate=true 时需要）..
     */
    @RequiresPermissions("mp:memberCard:setActivateUserForm")
    @Log(title = "设置会员卡激活的字段（会员卡设置：wx_activate=true 时需要）..", businessType = BusinessType.INSERT)
    @PostMapping("/setActivateUserForm/{id}")
    public AjaxResult setActivateUserForm(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        MemberCardActivateUserFormRequest userFormRequest = new MemberCardActivateUserFormRequest();
        MemberCardActivateUserFormResult wxMpMemberCardUserInfoResult  =  wxService.switchoverTo(appId).getMemberCardService().setActivateUserForm(userFormRequest);
        return AjaxResult.success(wxMpMemberCardUserInfoResult);
    }

    /**
     * 更新会员卡信息.
     */
    @RequiresPermissions("mp:memberCard:updateCardInfo")
    @Log(title = "更新会员卡信息.", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCardInfo/{id}")
    public AjaxResult updateCardInfo(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        MemberCardUpdateRequest memberCardUpdateRequest  = new MemberCardUpdateRequest();
        CardUpdateResult cardUpdateResult  =  wxService.switchoverTo(appId).getMemberCardService().updateCardInfo(memberCardUpdateRequest);
        return AjaxResult.success(cardUpdateResult);
    }


    /**
     * 当会员持卡消费后，支持开发者调用该接口更新会员信息.
     */
    @RequiresPermissions("mp:memberCard:updateUserMemberCard")
    @Log(title = "当会员持卡消费后，支持开发者调用该接口更新会员信息.", businessType = BusinessType.UPDATE)
    @PutMapping("/updateUserMemberCard/{id}")
    public AjaxResult updateUserMemberCard(@PathVariable Long id) throws WxErrorException {
        String appId = this.iMpAccountService.selectMpAccountById(id).getAppId();
        WxMpMemberCardUpdateMessage updateUserMessage = new WxMpMemberCardUpdateMessage();
        WxMpMemberCardUpdateResult wxMpMemberCardUpdateResult  =  wxService.switchoverTo(appId).getMemberCardService().updateUserMemberCard(updateUserMessage);
        return AjaxResult.success(wxMpMemberCardUpdateResult);
    }
}


